<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 18 章 快速上手案例 | Python 编程</title>
  <meta name="description" content="使用Python进行编程" />
  <meta name="generator" content="bookdown 0.17.2 and GitBook 2.6.7" />

  <meta property="og:title" content="第 18 章 快速上手案例 | Python 编程" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="使用Python进行编程" />
  <meta name="github-repo" content="yangjh-xbmu/learningpython" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 18 章 快速上手案例 | Python 编程" />
  
  <meta name="twitter:description" content="使用Python进行编程" />
  

<meta name="author" content="杨志宏" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="django.html"/>

<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css\style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">LearningPython</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Python 简介</a><ul>
<li class="chapter" data-level="0.1" data-path="index.html"><a href="index.html#python-发展历史"><i class="fa fa-check"></i><b>0.1</b> Python 发展历史</a></li>
<li class="chapter" data-level="0.2" data-path="index.html"><a href="index.html#python-特点"><i class="fa fa-check"></i><b>0.2</b> Python 特点</a></li>
<li class="chapter" data-level="0.3" data-path="index.html"><a href="index.html#使用-python-的知名项目"><i class="fa fa-check"></i><b>0.3</b> 使用 Python 的知名项目</a></li>
<li class="chapter" data-level="0.4" data-path="index.html"><a href="index.html#学习资源"><i class="fa fa-check"></i><b>0.4</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="ide.html"><a href="ide.html"><i class="fa fa-check"></i><b>1</b> Python 开发环境搭建</a><ul>
<li class="chapter" data-level="1.1" data-path="ide.html"><a href="ide.html#在-windows-中安装-python-3"><i class="fa fa-check"></i><b>1.1</b> 在 Windows 中安装 Python 3</a></li>
<li class="chapter" data-level="1.2" data-path="ide.html"><a href="ide.html#在-macos-中安装-python-3"><i class="fa fa-check"></i><b>1.2</b> 在 MacOS 中安装 Python 3</a><ul>
<li class="chapter" data-level="1.2.1" data-path="ide.html"><a href="ide.html#安装-xcode"><i class="fa fa-check"></i><b>1.2.1</b> 安装 Xcode</a></li>
<li class="chapter" data-level="1.2.2" data-path="ide.html"><a href="ide.html#安装-homebrew"><i class="fa fa-check"></i><b>1.2.2</b> 安装 Homebrew</a></li>
<li class="chapter" data-level="1.2.3" data-path="ide.html"><a href="ide.html#安装-python3"><i class="fa fa-check"></i><b>1.2.3</b> 安装 Python3</a></li>
<li class="chapter" data-level="1.2.4" data-path="ide.html"><a href="ide.html#使用-python3"><i class="fa fa-check"></i><b>1.2.4</b> 使用 Python3</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="ide.html"><a href="ide.html#使用-python-虚拟环境"><i class="fa fa-check"></i><b>1.3</b> 使用 python 虚拟环境</a><ul>
<li class="chapter" data-level="1.3.1" data-path="ide.html"><a href="ide.html#为什么要用虚拟环境"><i class="fa fa-check"></i><b>1.3.1</b> 为什么要用虚拟环境</a></li>
<li class="chapter" data-level="1.3.2" data-path="ide.html"><a href="ide.html#虚拟环境的创建与使用"><i class="fa fa-check"></i><b>1.3.2</b> 虚拟环境的创建与使用</a></li>
<li class="chapter" data-level="1.3.3" data-path="ide.html"><a href="ide.html#使用-pipenv-管理虚拟环境"><i class="fa fa-check"></i><b>1.3.3</b> 使用 pipenv 管理虚拟环境</a></li>
<li class="chapter" data-level="1.3.4" data-path="ide.html"><a href="ide.html#虚拟环境与系统环境的区别"><i class="fa fa-check"></i><b>1.3.4</b> 虚拟环境与系统环境的区别</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="ide.html"><a href="ide.html#编辑器"><i class="fa fa-check"></i><b>1.4</b> 编辑器</a><ul>
<li class="chapter" data-level="1.4.1" data-path="ide.html"><a href="ide.html#visual-studio-code-中的必要设置"><i class="fa fa-check"></i><b>1.4.1</b> Visual Studio Code 中的必要设置</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>I 核心语法</b></span></li>
<li class="chapter" data-level="2" data-path="systax.html"><a href="systax.html"><i class="fa fa-check"></i><b>2</b> 核心语法</a><ul>
<li class="chapter" data-level="2.1" data-path="systax.html"><a href="systax.html#注释"><i class="fa fa-check"></i><b>2.1</b> 注释</a></li>
<li class="chapter" data-level="2.2" data-path="systax.html"><a href="systax.html#变量"><i class="fa fa-check"></i><b>2.2</b> 变量</a><ul>
<li class="chapter" data-level="2.2.1" data-path="systax.html"><a href="systax.html#变量名称"><i class="fa fa-check"></i><b>2.2.1</b> 变量名称</a></li>
<li class="chapter" data-level="2.2.2" data-path="systax.html"><a href="systax.html#变量赋值"><i class="fa fa-check"></i><b>2.2.2</b> 变量赋值</a></li>
<li class="chapter" data-level="2.2.3" data-path="systax.html"><a href="systax.html#同步赋值"><i class="fa fa-check"></i><b>2.2.3</b> 同步赋值</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="systax.html"><a href="systax.html#数字类型"><i class="fa fa-check"></i><b>2.3</b> 数字类型</a><ul>
<li class="chapter" data-level="2.3.1" data-path="systax.html"><a href="systax.html#查看变量类型"><i class="fa fa-check"></i><b>2.3.1</b> 查看变量类型</a></li>
<li class="chapter" data-level="2.3.2" data-path="systax.html"><a href="systax.html#整型"><i class="fa fa-check"></i><b>2.3.2</b> 整型</a></li>
<li class="chapter" data-level="2.3.3" data-path="systax.html"><a href="systax.html#浮点类型"><i class="fa fa-check"></i><b>2.3.3</b> 浮点类型</a></li>
<li class="chapter" data-level="2.3.4" data-path="systax.html"><a href="systax.html#复数"><i class="fa fa-check"></i><b>2.3.4</b> 复数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="systax.html"><a href="systax.html#运算符"><i class="fa fa-check"></i><b>2.4</b> 运算符</a><ul>
<li class="chapter" data-level="2.4.1" data-path="systax.html"><a href="systax.html#运算符的优先级别"><i class="fa fa-check"></i><b>2.4.1</b> 运算符的优先级别</a></li>
<li class="chapter" data-level="2.4.2" data-path="systax.html"><a href="systax.html#增强赋值运算符"><i class="fa fa-check"></i><b>2.4.2</b> 增强赋值运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="systax.html"><a href="systax.html#序列"><i class="fa fa-check"></i><b>2.5</b> 序列</a><ul>
<li class="chapter" data-level="2.5.1" data-path="systax.html"><a href="systax.html#索引"><i class="fa fa-check"></i><b>2.5.1</b> 索引</a></li>
<li class="chapter" data-level="2.5.2" data-path="systax.html"><a href="systax.html#分片"><i class="fa fa-check"></i><b>2.5.2</b> 分片</a></li>
<li class="chapter" data-level="2.5.3" data-path="systax.html"><a href="systax.html#序列相加"><i class="fa fa-check"></i><b>2.5.3</b> 序列相加</a></li>
<li class="chapter" data-level="2.5.4" data-path="systax.html"><a href="systax.html#序列相乘"><i class="fa fa-check"></i><b>2.5.4</b> 序列相乘</a></li>
<li class="chapter" data-level="2.5.5" data-path="systax.html"><a href="systax.html#成员资格"><i class="fa fa-check"></i><b>2.5.5</b> 成员资格</a></li>
<li class="chapter" data-level="2.5.6" data-path="systax.html"><a href="systax.html#长度最小值最大值"><i class="fa fa-check"></i><b>2.5.6</b> 长度、最小值、最大值</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="systax.html"><a href="systax.html#字符串"><i class="fa fa-check"></i><b>2.6</b> 字符串</a><ul>
<li class="chapter" data-level="2.6.1" data-path="systax.html"><a href="systax.html#创建字符串"><i class="fa fa-check"></i><b>2.6.1</b> 创建字符串</a></li>
<li class="chapter" data-level="2.6.2" data-path="systax.html"><a href="systax.html#字符串的不可变性"><i class="fa fa-check"></i><b>2.6.2</b> 字符串的不可变性</a></li>
<li class="chapter" data-level="2.6.3" data-path="systax.html"><a href="systax.html#字符串操作"><i class="fa fa-check"></i><b>2.6.3</b> 字符串操作</a></li>
<li class="chapter" data-level="2.6.4" data-path="systax.html"><a href="systax.html#字符串分片"><i class="fa fa-check"></i><b>2.6.4</b> 字符串分片</a></li>
<li class="chapter" data-level="2.6.5" data-path="systax.html"><a href="systax.html#in-和-not-in-操作符"><i class="fa fa-check"></i><b>2.6.5</b> in 和 not in 操作符</a></li>
<li class="chapter" data-level="2.6.6" data-path="systax.html"><a href="systax.html#string-对象的方法"><i class="fa fa-check"></i><b>2.6.6</b> String 对象的方法</a></li>
<li class="chapter" data-level="2.6.7" data-path="systax.html"><a href="systax.html#比较字符串"><i class="fa fa-check"></i><b>2.6.7</b> 比较字符串</a></li>
<li class="chapter" data-level="2.6.8" data-path="systax.html"><a href="systax.html#遍历字符串"><i class="fa fa-check"></i><b>2.6.8</b> 遍历字符串</a></li>
<li class="chapter" data-level="2.6.9" data-path="systax.html"><a href="systax.html#字符串内容检验"><i class="fa fa-check"></i><b>2.6.9</b> 字符串内容检验</a></li>
<li class="chapter" data-level="2.6.10" data-path="systax.html"><a href="systax.html#在字符串内查找和替换"><i class="fa fa-check"></i><b>2.6.10</b> 在字符串内查找和替换</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="systax.html"><a href="systax.html#列表"><i class="fa fa-check"></i><b>2.7</b> 列表</a><ul>
<li class="chapter" data-level="2.7.1" data-path="systax.html"><a href="systax.html#列表赋值"><i class="fa fa-check"></i><b>2.7.1</b> 列表赋值</a></li>
<li class="chapter" data-level="2.7.2" data-path="systax.html"><a href="systax.html#删除元素"><i class="fa fa-check"></i><b>2.7.2</b> 删除元素</a></li>
<li class="chapter" data-level="2.7.3" data-path="systax.html"><a href="systax.html#分片赋值"><i class="fa fa-check"></i><b>2.7.3</b> 分片赋值</a></li>
<li class="chapter" data-level="2.7.4" data-path="systax.html"><a href="systax.html#列表对象常用内置方法"><i class="fa fa-check"></i><b>2.7.4</b> 列表对象常用内置方法</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="systax.html"><a href="systax.html#字典"><i class="fa fa-check"></i><b>2.8</b> 字典</a><ul>
<li class="chapter" data-level="2.8.1" data-path="systax.html"><a href="systax.html#创建字典"><i class="fa fa-check"></i><b>2.8.1</b> 创建字典</a></li>
<li class="chapter" data-level="2.8.2" data-path="systax.html"><a href="systax.html#获取修改和添加字典元素"><i class="fa fa-check"></i><b>2.8.2</b> 获取、修改和添加字典元素</a></li>
<li class="chapter" data-level="2.8.3" data-path="systax.html"><a href="systax.html#遍历字典"><i class="fa fa-check"></i><b>2.8.3</b> 遍历字典</a></li>
<li class="chapter" data-level="2.8.4" data-path="systax.html"><a href="systax.html#字典常用方法"><i class="fa fa-check"></i><b>2.8.4</b> 字典常用方法</a></li>
<li class="chapter" data-level="2.8.5" data-path="systax.html"><a href="systax.html#字典的排序"><i class="fa fa-check"></i><b>2.8.5</b> 字典的排序</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="systax.html"><a href="systax.html#元组"><i class="fa fa-check"></i><b>2.9</b> 元组</a><ul>
<li class="chapter" data-level="2.9.1" data-path="systax.html"><a href="systax.html#创建元组"><i class="fa fa-check"></i><b>2.9.1</b> 创建元组</a></li>
<li class="chapter" data-level="2.9.2" data-path="systax.html"><a href="systax.html#元组相关方法"><i class="fa fa-check"></i><b>2.9.2</b> 元组相关方法</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="systax.html"><a href="systax.html#控制声明"><i class="fa fa-check"></i><b>2.10</b> 控制声明</a><ul>
<li class="chapter" data-level="2.10.1" data-path="systax.html"><a href="systax.html#分支判断"><i class="fa fa-check"></i><b>2.10.1</b> 分支判断</a></li>
<li class="chapter" data-level="2.10.2" data-path="systax.html"><a href="systax.html#分支嵌套"><i class="fa fa-check"></i><b>2.10.2</b> 分支嵌套</a></li>
<li class="chapter" data-level="2.10.3" data-path="systax.html"><a href="systax.html#三元运算符"><i class="fa fa-check"></i><b>2.10.3</b> 三元运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.11" data-path="systax.html"><a href="systax.html#循环"><i class="fa fa-check"></i><b>2.11</b> 循环</a><ul>
<li class="chapter" data-level="2.11.1" data-path="systax.html"><a href="systax.html#for-循环"><i class="fa fa-check"></i><b>2.11.1</b> for 循环</a></li>
<li class="chapter" data-level="2.11.2" data-path="systax.html"><a href="systax.html#范围循环"><i class="fa fa-check"></i><b>2.11.2</b> 范围循环</a></li>
<li class="chapter" data-level="2.11.3" data-path="systax.html"><a href="systax.html#while-循环"><i class="fa fa-check"></i><b>2.11.3</b> while 循环</a></li>
<li class="chapter" data-level="2.11.4" data-path="systax.html"><a href="systax.html#中断循环"><i class="fa fa-check"></i><b>2.11.4</b> 中断循环</a></li>
<li class="chapter" data-level="2.11.5" data-path="systax.html"><a href="systax.html#继续循环"><i class="fa fa-check"></i><b>2.11.5</b> 继续循环</a></li>
</ul></li>
<li class="chapter" data-level="2.12" data-path="systax.html"><a href="systax.html#函数"><i class="fa fa-check"></i><b>2.12</b> 函数</a><ul>
<li class="chapter" data-level="2.12.1" data-path="systax.html"><a href="systax.html#创建函数"><i class="fa fa-check"></i><b>2.12.1</b> 创建函数</a></li>
<li class="chapter" data-level="2.12.2" data-path="systax.html"><a href="systax.html#函数返回值"><i class="fa fa-check"></i><b>2.12.2</b> 函数返回值</a></li>
<li class="chapter" data-level="2.12.3" data-path="systax.html"><a href="systax.html#全局变量和局域变量"><i class="fa fa-check"></i><b>2.12.3</b> 全局变量和局域变量</a></li>
<li class="chapter" data-level="2.12.4" data-path="systax.html"><a href="systax.html#参数的默认值"><i class="fa fa-check"></i><b>2.12.4</b> 参数的默认值</a></li>
<li class="chapter" data-level="2.12.5" data-path="systax.html"><a href="systax.html#关键字参数"><i class="fa fa-check"></i><b>2.12.5</b> 关键字参数</a></li>
<li class="chapter" data-level="2.12.6" data-path="systax.html"><a href="systax.html#返回多个值"><i class="fa fa-check"></i><b>2.12.6</b> 返回多个值</a></li>
<li class="chapter" data-level="2.12.7" data-path="systax.html"><a href="systax.html#函数文档字符串"><i class="fa fa-check"></i><b>2.12.7</b> 函数文档字符串</a></li>
<li class="chapter" data-level="2.12.8" data-path="systax.html"><a href="systax.html#lambda表达式"><i class="fa fa-check"></i><b>2.12.8</b> lambda表达式</a></li>
<li class="chapter" data-level="2.12.9" data-path="systax.html"><a href="systax.html#args-和-kwargs"><i class="fa fa-check"></i><b>2.12.9</b> <code>*args</code> 和 <code>**kwargs</code></a></li>
<li class="chapter" data-level="2.12.10" data-path="systax.html"><a href="systax.html#参考资料"><i class="fa fa-check"></i><b>2.12.10</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="oop.html"><a href="oop.html"><i class="fa fa-check"></i><b>3</b> Python中的面向对象编程</a><ul>
<li class="chapter" data-level="3.1" data-path="oop.html"><a href="oop.html#python对象和类"><i class="fa fa-check"></i><b>3.1</b> Python对象和类</a><ul>
<li class="chapter" data-level="3.1.1" data-path="oop.html"><a href="oop.html#创建类"><i class="fa fa-check"></i><b>3.1.1</b> 创建类</a></li>
<li class="chapter" data-level="3.1.2" data-path="oop.html"><a href="oop.html#从类中创建对象"><i class="fa fa-check"></i><b>3.1.2</b> 从类中创建对象</a></li>
<li class="chapter" data-level="3.1.3" data-path="oop.html"><a href="oop.html#隐藏数据字段"><i class="fa fa-check"></i><b>3.1.3</b> 隐藏数据字段</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="oop.html"><a href="oop.html#操作符重载"><i class="fa fa-check"></i><b>3.2</b> 操作符重载</a></li>
<li class="chapter" data-level="3.3" data-path="oop.html"><a href="oop.html#继承和多态"><i class="fa fa-check"></i><b>3.3</b> 继承和多态</a><ul>
<li class="chapter" data-level="3.3.1" data-path="oop.html"><a href="oop.html#多重继承"><i class="fa fa-check"></i><b>3.3.1</b> 多重继承</a></li>
<li class="chapter" data-level="3.3.2" data-path="oop.html"><a href="oop.html#重写方法"><i class="fa fa-check"></i><b>3.3.2</b> 重写方法</a></li>
<li class="chapter" data-level="3.3.3" data-path="oop.html"><a href="oop.html#判断对象是否属于某类"><i class="fa fa-check"></i><b>3.3.3</b> 判断对象是否属于某类</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="exception.html"><a href="exception.html"><i class="fa fa-check"></i><b>4</b> 异常处理</a><ul>
<li class="chapter" data-level="4.1" data-path="exception.html"><a href="exception.html#捕获异常"><i class="fa fa-check"></i><b>4.1</b> 捕获异常</a><ul>
<li class="chapter" data-level="4.1.1" data-path="exception.html"><a href="exception.html#try-except"><i class="fa fa-check"></i><b>4.1.1</b> try-except</a></li>
<li class="chapter" data-level="4.1.2" data-path="exception.html"><a href="exception.html#多个except"><i class="fa fa-check"></i><b>4.1.2</b> 多个except</a></li>
<li class="chapter" data-level="4.1.3" data-path="exception.html"><a href="exception.html#自定义异常"><i class="fa fa-check"></i><b>4.1.3</b> 自定义异常</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="decorator.html"><a href="decorator.html"><i class="fa fa-check"></i><b>5</b> Python 装饰器</a><ul>
<li class="chapter" data-level="5.1" data-path="decorator.html"><a href="decorator.html#为什么要用装饰器"><i class="fa fa-check"></i><b>5.1</b> 为什么要用装饰器</a></li>
<li class="chapter" data-level="5.2" data-path="decorator.html"><a href="decorator.html#简单装饰器"><i class="fa fa-check"></i><b>5.2</b> 简单装饰器</a></li>
<li class="chapter" data-level="5.3" data-path="decorator.html"><a href="decorator.html#语法糖"><i class="fa fa-check"></i><b>5.3</b> @ 语法糖</a></li>
<li class="chapter" data-level="5.4" data-path="decorator.html"><a href="decorator.html#argskwargs"><i class="fa fa-check"></i><b>5.4</b> <code>*args</code>、<code>**kwargs</code></a></li>
<li class="chapter" data-level="5.5" data-path="decorator.html"><a href="decorator.html#带参数的装饰器"><i class="fa fa-check"></i><b>5.5</b> 带参数的装饰器</a></li>
<li class="chapter" data-level="5.6" data-path="decorator.html"><a href="decorator.html#类装饰器"><i class="fa fa-check"></i><b>5.6</b> 类装饰器</a></li>
<li class="chapter" data-level="5.7" data-path="decorator.html"><a href="decorator.html#functools.wraps"><i class="fa fa-check"></i><b>5.7</b> functools.wraps</a></li>
<li class="chapter" data-level="5.8" data-path="decorator.html"><a href="decorator.html#装饰器顺序"><i class="fa fa-check"></i><b>5.8</b> 装饰器顺序</a></li>
<li class="chapter" data-level="" data-path="decorator.html"><a href="decorator.html#参考资料-1"><i class="fa fa-check"></i>参考资料</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="module.html"><a href="module.html"><i class="fa fa-check"></i><b>6</b> 模块</a><ul>
<li class="chapter" data-level="6.1" data-path="module.html"><a href="module.html#创建模块"><i class="fa fa-check"></i><b>6.1</b> 创建模块</a></li>
<li class="chapter" data-level="6.2" data-path="module.html"><a href="module.html#使用模块中的指定内容"><i class="fa fa-check"></i><b>6.2</b> 使用模块中的指定内容</a></li>
<li class="chapter" data-level="6.3" data-path="module.html"><a href="module.html#dir函数"><i class="fa fa-check"></i><b>6.3</b> dir函数</a></li>
<li class="chapter" data-level="6.4" data-path="module.html"><a href="module.html#包"><i class="fa fa-check"></i><b>6.4</b> 包</a></li>
</ul></li>
<li class="part"><span><b>II 进阶</b></span></li>
<li class="chapter" data-level="7" data-path="buildinfunctions.html"><a href="buildinfunctions.html"><i class="fa fa-check"></i><b>7</b> 内置函数</a><ul>
<li class="chapter" data-level="7.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#dict函数"><i class="fa fa-check"></i><b>7.1</b> dict函数</a><ul>
<li class="chapter" data-level="7.1.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述"><i class="fa fa-check"></i><b>7.1.1</b> 描述</a></li>
<li class="chapter" data-level="7.1.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法"><i class="fa fa-check"></i><b>7.1.2</b> 语法</a></li>
<li class="chapter" data-level="7.1.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值"><i class="fa fa-check"></i><b>7.1.3</b> 返回值</a></li>
<li class="chapter" data-level="7.1.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例"><i class="fa fa-check"></i><b>7.1.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#zip函数"><i class="fa fa-check"></i><b>7.2</b> zip函数</a><ul>
<li class="chapter" data-level="7.2.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-1"><i class="fa fa-check"></i><b>7.2.1</b> 描述</a></li>
<li class="chapter" data-level="7.2.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-1"><i class="fa fa-check"></i><b>7.2.2</b> 语法</a></li>
<li class="chapter" data-level="7.2.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-1"><i class="fa fa-check"></i><b>7.2.3</b> 返回值</a></li>
<li class="chapter" data-level="7.2.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#特殊用法"><i class="fa fa-check"></i><b>7.2.4</b> 特殊用法</a></li>
<li class="chapter" data-level="7.2.5" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-1"><i class="fa fa-check"></i><b>7.2.5</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#list函数"><i class="fa fa-check"></i><b>7.3</b> list函数</a><ul>
<li class="chapter" data-level="7.3.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-2"><i class="fa fa-check"></i><b>7.3.1</b> 描述</a></li>
<li class="chapter" data-level="7.3.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-2"><i class="fa fa-check"></i><b>7.3.2</b> 语法</a></li>
<li class="chapter" data-level="7.3.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-2"><i class="fa fa-check"></i><b>7.3.3</b> 返回值</a></li>
<li class="chapter" data-level="7.3.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-2"><i class="fa fa-check"></i><b>7.3.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#min函数"><i class="fa fa-check"></i><b>7.4</b> min函数</a><ul>
<li class="chapter" data-level="7.4.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-3"><i class="fa fa-check"></i><b>7.4.1</b> 描述</a></li>
<li class="chapter" data-level="7.4.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-3"><i class="fa fa-check"></i><b>7.4.2</b> 语法</a></li>
<li class="chapter" data-level="7.4.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-3"><i class="fa fa-check"></i><b>7.4.3</b> 返回值</a></li>
<li class="chapter" data-level="7.4.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-3"><i class="fa fa-check"></i><b>7.4.4</b> 实例</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="iterator.html"><a href="iterator.html"><i class="fa fa-check"></i><b>8</b> Python可迭代对象</a><ul>
<li class="chapter" data-level="8.1" data-path="iterator.html"><a href="iterator.html#可迭代对象"><i class="fa fa-check"></i><b>8.1</b> 可迭代对象</a></li>
<li class="chapter" data-level="8.2" data-path="iterator.html"><a href="iterator.html#迭代器"><i class="fa fa-check"></i><b>8.2</b> 迭代器</a></li>
<li class="chapter" data-level="8.3" data-path="iterator.html"><a href="iterator.html#生成器"><i class="fa fa-check"></i><b>8.3</b> 生成器</a></li>
<li class="chapter" data-level="8.4" data-path="iterator.html"><a href="iterator.html#三者之间关系"><i class="fa fa-check"></i><b>8.4</b> 三者之间关系</a></li>
<li class="chapter" data-level="8.5" data-path="iterator.html"><a href="iterator.html#迭代器长度的计算"><i class="fa fa-check"></i><b>8.5</b> 迭代器长度的计算</a></li>
<li class="chapter" data-level="8.6" data-path="iterator.html"><a href="iterator.html#yield"><i class="fa fa-check"></i><b>8.6</b> yield</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="underline.html"><a href="underline.html"><i class="fa fa-check"></i><b>9</b> 单、双下划线的区别</a><ul>
<li class="chapter" data-level="9.1" data-path="underline.html"><a href="underline.html#单下划线开头"><i class="fa fa-check"></i><b>9.1</b> 单下划线开头</a></li>
<li class="chapter" data-level="9.2" data-path="underline.html"><a href="underline.html#双下划线开头"><i class="fa fa-check"></i><b>9.2</b> 双下划线开头</a></li>
<li class="chapter" data-level="9.3" data-path="underline.html"><a href="underline.html#双下划线开头和结尾"><i class="fa fa-check"></i><b>9.3</b> 双下划线开头和结尾</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="re.html"><a href="re.html"><i class="fa fa-check"></i><b>10</b> 正则表达式</a><ul>
<li class="chapter" data-level="10.1" data-path="re.html"><a href="re.html#正则表达式发展简史"><i class="fa fa-check"></i><b>10.1</b> 正则表达式发展简史</a></li>
<li class="chapter" data-level="10.2" data-path="re.html"><a href="re.html#python中使用正则表达式的流程"><i class="fa fa-check"></i><b>10.2</b> Python中使用正则表达式的流程</a></li>
<li class="chapter" data-level="10.3" data-path="re.html"><a href="re.html#正则表达式的构成要素"><i class="fa fa-check"></i><b>10.3</b> 正则表达式的构成要素</a><ul>
<li class="chapter" data-level="10.3.1" data-path="re.html"><a href="re.html#定界符"><i class="fa fa-check"></i><b>10.3.1</b> 定界符</a></li>
<li class="chapter" data-level="10.3.2" data-path="re.html"><a href="re.html#原子"><i class="fa fa-check"></i><b>10.3.2</b> 原子</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="re.html"><a href="re.html#元字符"><i class="fa fa-check"></i><b>10.4</b> 元字符</a><ul>
<li class="chapter" data-level="10.4.1" data-path="re.html"><a href="re.html#匹配单个字符的元字符"><i class="fa fa-check"></i><b>10.4.1</b> 匹配单个字符的元字符</a></li>
<li class="chapter" data-level="10.4.2" data-path="re.html"><a href="re.html#常用转义符"><i class="fa fa-check"></i><b>10.4.2</b> 常用转义符</a></li>
<li class="chapter" data-level="10.4.3" data-path="re.html"><a href="re.html#提供计数功能的元字符"><i class="fa fa-check"></i><b>10.4.3</b> 提供计数功能的元字符</a></li>
<li class="chapter" data-level="10.4.4" data-path="re.html"><a href="re.html#匹配位置的元字符"><i class="fa fa-check"></i><b>10.4.4</b> 匹配位置的元字符</a></li>
<li class="chapter" data-level="10.4.5" data-path="re.html"><a href="re.html#其他元字符"><i class="fa fa-check"></i><b>10.4.5</b> 其他元字符</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="re.html"><a href="re.html#模式修饰符"><i class="fa fa-check"></i><b>10.5</b> 模式修饰符</a></li>
<li class="chapter" data-level="10.6" data-path="re.html"><a href="re.html#在python中使用正则表达式"><i class="fa fa-check"></i><b>10.6</b> 在Python中使用正则表达式</a><ul>
<li class="chapter" data-level="10.6.1" data-path="re.html"><a href="re.html#search"><i class="fa fa-check"></i><b>10.6.1</b> search()</a></li>
<li class="chapter" data-level="10.6.2" data-path="re.html"><a href="re.html#compile"><i class="fa fa-check"></i><b>10.6.2</b> compile()</a></li>
<li class="chapter" data-level="10.6.3" data-path="re.html"><a href="re.html#findall"><i class="fa fa-check"></i><b>10.6.3</b> findall()</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="argparse.html"><a href="argparse.html"><i class="fa fa-check"></i><b>11</b> 命令行参数模块argparse</a><ul>
<li class="chapter" data-level="11.1" data-path="argparse.html"><a href="argparse.html#创建解析器"><i class="fa fa-check"></i><b>11.1</b> 创建解析器</a></li>
<li class="chapter" data-level="11.2" data-path="argparse.html"><a href="argparse.html#添加参数选项"><i class="fa fa-check"></i><b>11.2</b> 添加参数选项</a><ul>
<li class="chapter" data-level="11.2.1" data-path="argparse.html"><a href="argparse.html#name-or-flags"><i class="fa fa-check"></i><b>11.2.1</b> name or flags</a></li>
<li class="chapter" data-level="11.2.2" data-path="argparse.html"><a href="argparse.html#help"><i class="fa fa-check"></i><b>11.2.2</b> help</a></li>
<li class="chapter" data-level="11.2.3" data-path="argparse.html"><a href="argparse.html#default和type"><i class="fa fa-check"></i><b>11.2.3</b> default和type</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="argparse.html"><a href="argparse.html#参数解析"><i class="fa fa-check"></i><b>11.3</b> 参数解析</a></li>
<li class="chapter" data-level="11.4" data-path="argparse.html"><a href="argparse.html#小结"><i class="fa fa-check"></i><b>11.4</b> 小结</a></li>
<li class="chapter" data-level="11.5" data-path="argparse.html"><a href="argparse.html#参考文献"><i class="fa fa-check"></i><b>11.5</b> 参考文献</a></li>
</ul></li>
<li class="part"><span><b>III 数据抓取</b></span></li>
<li class="chapter" data-level="12" data-path="spider.html"><a href="spider.html"><i class="fa fa-check"></i><b>12</b> 网络爬虫</a><ul>
<li class="chapter" data-level="12.1" data-path="spider.html"><a href="spider.html#网络爬虫原理"><i class="fa fa-check"></i><b>12.1</b> 网络爬虫原理</a></li>
<li class="chapter" data-level="12.2" data-path="spider.html"><a href="spider.html#http请求原理"><i class="fa fa-check"></i><b>12.2</b> HTTP请求原理</a><ul>
<li class="chapter" data-level="12.2.1" data-path="spider.html"><a href="spider.html#url"><i class="fa fa-check"></i><b>12.2.1</b> URL</a></li>
<li class="chapter" data-level="12.2.2" data-path="spider.html"><a href="spider.html#请求方式"><i class="fa fa-check"></i><b>12.2.2</b> 请求方式</a></li>
<li class="chapter" data-level="12.2.3" data-path="spider.html"><a href="spider.html#requests包"><i class="fa fa-check"></i><b>12.2.3</b> requests包</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="spider.html"><a href="spider.html#编码"><i class="fa fa-check"></i><b>12.3</b> 编码</a><ul>
<li class="chapter" data-level="12.3.1" data-path="spider.html"><a href="spider.html#编码方式"><i class="fa fa-check"></i><b>12.3.1</b> 编码方式</a></li>
<li class="chapter" data-level="12.3.2" data-path="spider.html"><a href="spider.html#编码转换"><i class="fa fa-check"></i><b>12.3.2</b> 编码转换</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="spider.html"><a href="spider.html#存储"><i class="fa fa-check"></i><b>12.4</b> 存储</a><ul>
<li class="chapter" data-level="12.4.1" data-path="spider.html"><a href="spider.html#存储到文件"><i class="fa fa-check"></i><b>12.4.1</b> 存储到文件</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="13" data-path="httplib.html"><a href="httplib.html"><i class="fa fa-check"></i><b>13</b> HTTP库</a><ul>
<li class="chapter" data-level="13.1" data-path="httplib.html"><a href="httplib.html#urllib"><i class="fa fa-check"></i><b>13.1</b> urllib</a><ul>
<li class="chapter" data-level="13.1.1" data-path="httplib.html"><a href="httplib.html#发送请求"><i class="fa fa-check"></i><b>13.1.1</b> 发送请求</a></li>
<li class="chapter" data-level="13.1.2" data-path="httplib.html"><a href="httplib.html#处理异常"><i class="fa fa-check"></i><b>13.1.2</b> 处理异常</a></li>
<li class="chapter" data-level="13.1.3" data-path="httplib.html"><a href="httplib.html#解析链接"><i class="fa fa-check"></i><b>13.1.3</b> 解析链接</a></li>
</ul></li>
<li class="chapter" data-level="13.2" data-path="httplib.html"><a href="httplib.html#使用requests"><i class="fa fa-check"></i><b>13.2</b> 使用requests</a><ul>
<li class="chapter" data-level="13.2.1" data-path="httplib.html"><a href="httplib.html#安装requests"><i class="fa fa-check"></i><b>13.2.1</b> 安装requests</a></li>
<li class="chapter" data-level="13.2.2" data-path="httplib.html"><a href="httplib.html#发起请求"><i class="fa fa-check"></i><b>13.2.2</b> 发起请求</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="httplib.html"><a href="httplib.html#网页编码检测及转换"><i class="fa fa-check"></i><b>13.3</b> 网页编码检测及转换</a></li>
<li class="chapter" data-level="13.4" data-path="httplib.html"><a href="httplib.html#参考资料-2"><i class="fa fa-check"></i><b>13.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="pyquery.html"><a href="pyquery.html"><i class="fa fa-check"></i><b>14</b> Pyquery</a><ul>
<li class="chapter" data-level="14.1" data-path="pyquery.html"><a href="pyquery.html#安装"><i class="fa fa-check"></i><b>14.1</b> 安装</a></li>
<li class="chapter" data-level="14.2" data-path="pyquery.html"><a href="pyquery.html#初始化"><i class="fa fa-check"></i><b>14.2</b> 初始化</a></li>
<li class="chapter" data-level="14.3" data-path="pyquery.html"><a href="pyquery.html#获取信息"><i class="fa fa-check"></i><b>14.3</b> 获取信息</a><ul>
<li class="chapter" data-level="14.3.1" data-path="pyquery.html"><a href="pyquery.html#通过选择符选定元素"><i class="fa fa-check"></i><b>14.3.1</b> 通过选择符选定元素</a></li>
<li class="chapter" data-level="14.3.2" data-path="pyquery.html"><a href="pyquery.html#通过迭代获取最终结果"><i class="fa fa-check"></i><b>14.3.2</b> 通过迭代获取最终结果</a></li>
</ul></li>
<li class="chapter" data-level="14.4" data-path="pyquery.html"><a href="pyquery.html#参考资料-3"><i class="fa fa-check"></i><b>14.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="databases.html"><a href="databases.html"><i class="fa fa-check"></i><b>15</b> 数据存储</a><ul>
<li class="chapter" data-level="15.1" data-path="databases.html"><a href="databases.html#mysql的存储"><i class="fa fa-check"></i><b>15.1</b> MySQL的存储</a><ul>
<li class="chapter" data-level="15.1.1" data-path="databases.html"><a href="databases.html#安装pymysql和mysql"><i class="fa fa-check"></i><b>15.1.1</b> 安装PyMySQL和MySQL</a></li>
<li class="chapter" data-level="15.1.2" data-path="databases.html"><a href="databases.html#连接数据库"><i class="fa fa-check"></i><b>15.1.2</b> 连接数据库</a></li>
<li class="chapter" data-level="15.1.3" data-path="databases.html"><a href="databases.html#对数据库进行操作"><i class="fa fa-check"></i><b>15.1.3</b> 对数据库进行操作</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="databases.html"><a href="databases.html#mongodb"><i class="fa fa-check"></i><b>15.2</b> MongoDB</a><ul>
<li class="chapter" data-level="15.2.1" data-path="databases.html"><a href="databases.html#在macos中的安装"><i class="fa fa-check"></i><b>15.2.1</b> 在macOS中的安装</a></li>
<li class="chapter" data-level="15.2.2" data-path="databases.html"><a href="databases.html#连接mongodb"><i class="fa fa-check"></i><b>15.2.2</b> 连接MongoDB</a></li>
<li class="chapter" data-level="15.2.3" data-path="databases.html"><a href="databases.html#指定数据库"><i class="fa fa-check"></i><b>15.2.3</b> 指定数据库</a></li>
<li class="chapter" data-level="15.2.4" data-path="databases.html"><a href="databases.html#指定集合"><i class="fa fa-check"></i><b>15.2.4</b> 指定集合</a></li>
<li class="chapter" data-level="15.2.5" data-path="databases.html"><a href="databases.html#插入数据"><i class="fa fa-check"></i><b>15.2.5</b> 插入数据</a></li>
<li class="chapter" data-level="15.2.6" data-path="databases.html"><a href="databases.html#查询数据-1"><i class="fa fa-check"></i><b>15.2.6</b> 查询数据</a></li>
<li class="chapter" data-level="15.2.7" data-path="databases.html"><a href="databases.html#计数-1"><i class="fa fa-check"></i><b>15.2.7</b> 计数</a></li>
<li class="chapter" data-level="15.2.8" data-path="databases.html"><a href="databases.html#排序"><i class="fa fa-check"></i><b>15.2.8</b> 排序</a></li>
<li class="chapter" data-level="15.2.9" data-path="databases.html"><a href="databases.html#偏移和限定"><i class="fa fa-check"></i><b>15.2.9</b> 偏移和限定</a></li>
<li class="chapter" data-level="15.2.10" data-path="databases.html"><a href="databases.html#更新"><i class="fa fa-check"></i><b>15.2.10</b> 更新</a></li>
<li class="chapter" data-level="15.2.11" data-path="databases.html"><a href="databases.html#删除"><i class="fa fa-check"></i><b>15.2.11</b> 删除</a></li>
<li class="chapter" data-level="15.2.12" data-path="databases.html"><a href="databases.html#其他操作"><i class="fa fa-check"></i><b>15.2.12</b> 其他操作</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="databases.html"><a href="databases.html#参考资料-4"><i class="fa fa-check"></i><b>15.3</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="selenium.html"><a href="selenium.html"><i class="fa fa-check"></i><b>16</b> 使用Selenium抓取动态渲染页面</a><ul>
<li class="chapter" data-level="16.1" data-path="selenium.html"><a href="selenium.html#安装-1"><i class="fa fa-check"></i><b>16.1</b> 安装</a></li>
<li class="chapter" data-level="16.2" data-path="selenium.html"><a href="selenium.html#selenium的使用"><i class="fa fa-check"></i><b>16.2</b> Selenium的使用</a><ul>
<li class="chapter" data-level="16.2.1" data-path="selenium.html"><a href="selenium.html#初始化浏览器对象"><i class="fa fa-check"></i><b>16.2.1</b> 初始化浏览器对象</a></li>
<li class="chapter" data-level="16.2.2" data-path="selenium.html"><a href="selenium.html#访问页面"><i class="fa fa-check"></i><b>16.2.2</b> 访问页面</a></li>
<li class="chapter" data-level="16.2.3" data-path="selenium.html"><a href="selenium.html#查找节点"><i class="fa fa-check"></i><b>16.2.3</b> 查找节点</a></li>
<li class="chapter" data-level="16.2.4" data-path="selenium.html"><a href="selenium.html#节点操作"><i class="fa fa-check"></i><b>16.2.4</b> 节点操作</a></li>
<li class="chapter" data-level="16.2.5" data-path="selenium.html"><a href="selenium.html#执行javascript"><i class="fa fa-check"></i><b>16.2.5</b> 执行JavaScript</a></li>
<li class="chapter" data-level="16.2.6" data-path="selenium.html"><a href="selenium.html#获取节点信息"><i class="fa fa-check"></i><b>16.2.6</b> 获取节点信息</a></li>
<li class="chapter" data-level="16.2.7" data-path="selenium.html"><a href="selenium.html#延时等待"><i class="fa fa-check"></i><b>16.2.7</b> 延时等待</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="selenium.html"><a href="selenium.html#扩展阅读"><i class="fa fa-check"></i><b>16.3</b> 扩展阅读</a></li>
</ul></li>
<li class="part"><span><b>IV 网络应用开发</b></span></li>
<li class="chapter" data-level="17" data-path="django.html"><a href="django.html"><i class="fa fa-check"></i><b>17</b> Django 框架的安装</a><ul>
<li class="chapter" data-level="17.1" data-path="django.html"><a href="django.html#安装-2"><i class="fa fa-check"></i><b>17.1</b> 安装</a><ul>
<li class="chapter" data-level="17.1.1" data-path="django.html"><a href="django.html#安装-django"><i class="fa fa-check"></i><b>17.1.1</b> 安装 Django</a></li>
<li class="chapter" data-level="17.1.2" data-path="django.html"><a href="django.html#验证安装"><i class="fa fa-check"></i><b>17.1.2</b> 验证安装</a></li>
<li class="chapter" data-level="17.1.3" data-path="django.html"><a href="django.html#创建项目并预览效果"><i class="fa fa-check"></i><b>17.1.3</b> 创建项目并预览效果</a></li>
</ul></li>
<li class="chapter" data-level="17.2" data-path="django.html"><a href="django.html#学习资源-1"><i class="fa fa-check"></i><b>17.2</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="quicktutorial.html"><a href="quicktutorial.html"><i class="fa fa-check"></i><b>18</b> 快速上手案例</a><ul>
<li class="chapter" data-level="18.1" data-path="quicktutorial.html"><a href="quicktutorial.html#在-django-项目中创建-app"><i class="fa fa-check"></i><b>18.1</b> 在 Django 项目中创建 APP</a><ul>
<li class="chapter" data-level="18.1.1" data-path="quicktutorial.html"><a href="quicktutorial.html#初始化-app"><i class="fa fa-check"></i><b>18.1.1</b> 初始化 APP</a></li>
<li class="chapter" data-level="18.1.2" data-path="quicktutorial.html"><a href="quicktutorial.html#将应用添加到项目中"><i class="fa fa-check"></i><b>18.1.2</b> 将应用添加到项目中</a></li>
<li class="chapter" data-level="18.1.3" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图"><i class="fa fa-check"></i><b>18.1.3</b> 创建视图</a></li>
<li class="chapter" data-level="18.1.4" data-path="quicktutorial.html"><a href="quicktutorial.html#规划路由"><i class="fa fa-check"></i><b>18.1.4</b> 规划路由</a></li>
<li class="chapter" data-level="18.1.5" data-path="quicktutorial.html"><a href="quicktutorial.html#预览效果"><i class="fa fa-check"></i><b>18.1.5</b> 预览效果</a></li>
</ul></li>
<li class="chapter" data-level="18.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模型的创建及使用"><i class="fa fa-check"></i><b>18.2</b> 模型的创建及使用</a><ul>
<li class="chapter" data-level="18.2.1" data-path="quicktutorial.html"><a href="quicktutorial.html#配置项目信息"><i class="fa fa-check"></i><b>18.2.1</b> 配置项目信息</a></li>
<li class="chapter" data-level="18.2.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型"><i class="fa fa-check"></i><b>18.2.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.2.3" data-path="quicktutorial.html"><a href="quicktutorial.html#激活模型"><i class="fa fa-check"></i><b>18.2.3</b> 激活模型</a></li>
<li class="chapter" data-level="18.2.4" data-path="quicktutorial.html"><a href="quicktutorial.html#数据库的生成与迁移"><i class="fa fa-check"></i><b>18.2.4</b> 数据库的生成与迁移</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="quicktutorial.html"><a href="quicktutorial.html#后台管理"><i class="fa fa-check"></i><b>18.3</b> 后台管理</a><ul>
<li class="chapter" data-level="18.3.1" data-path="quicktutorial.html"><a href="quicktutorial.html#创建管理员账号"><i class="fa fa-check"></i><b>18.3.1</b> 创建管理员账号</a></li>
<li class="chapter" data-level="18.3.2" data-path="quicktutorial.html"><a href="quicktutorial.html#添加引用到后台管理"><i class="fa fa-check"></i><b>18.3.2</b> 添加引用到后台管理</a></li>
<li class="chapter" data-level="18.3.3" data-path="quicktutorial.html"><a href="quicktutorial.html#进入后台管理界面"><i class="fa fa-check"></i><b>18.3.3</b> 进入后台管理界面</a></li>
</ul></li>
<li class="chapter" data-level="18.4" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图-1"><i class="fa fa-check"></i><b>18.4</b> 创建视图</a><ul>
<li class="chapter" data-level="18.4.1" data-path="quicktutorial.html"><a href="quicktutorial.html#添加多个视图"><i class="fa fa-check"></i><b>18.4.1</b> 添加多个视图</a></li>
<li class="chapter" data-level="18.4.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模版命名空间"><i class="fa fa-check"></i><b>18.4.2</b> 模版命名空间</a></li>
<li class="chapter" data-level="18.4.3" data-path="quicktutorial.html"><a href="quicktutorial.html#在视图中使用模板"><i class="fa fa-check"></i><b>18.4.3</b> 在视图中使用模板</a></li>
<li class="chapter" data-level="18.4.4" data-path="quicktutorial.html"><a href="quicktutorial.html#url命名空间"><i class="fa fa-check"></i><b>18.4.4</b> URL命名空间</a></li>
</ul></li>
<li class="chapter" data-level="18.5" data-path="quicktutorial.html"><a href="quicktutorial.html#模型"><i class="fa fa-check"></i><b>18.5</b> 模型</a><ul>
<li class="chapter" data-level="18.5.1" data-path="quicktutorial.html"><a href="quicktutorial.html#设置数据库信息-1"><i class="fa fa-check"></i><b>18.5.1</b> 设置数据库信息</a></li>
<li class="chapter" data-level="18.5.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型-1"><i class="fa fa-check"></i><b>18.5.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.5.3" data-path="quicktutorial.html"><a href="quicktutorial.html#使用模型"><i class="fa fa-check"></i><b>18.5.3</b> 使用模型</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="quicktutorial.html"><a href="quicktutorial.html#参考文献-1"><i class="fa fa-check"></i><b>18.6</b> 参考文献</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python 编程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="quicktutorial" class="section level1">
<h1><span class="header-section-number">第 18 章</span> 快速上手案例</h1>
<p>学习，也应该和敏捷开发一样，优先掌握最核心的概念，在最短的时间里，就能实践所学内容，做出可用的项目。然后再不断深入学习，逐步提高。这样学习者就能体会到学习的乐趣，也就能坚持学习。</p>
<p>最小可用是关键。故参考官方网站提供的教程，在其基础上精简内容，达到尽快入门的目的。</p>
<div id="在-django-项目中创建-app" class="section level2">
<h2><span class="header-section-number">18.1</span> 在 Django 项目中创建 APP</h2>
<p>在 Django 框架中，一个<strong>应用</strong> (app) 实现某个专门的功能（如投票、博客），而<strong>项目</strong>（project）可以由多个 app 和配置文件的集合构成，多个 app 组成完整站点的功能。而且，App 还可以在多个 project 中共享。</p>
<div id="初始化-app" class="section level3">
<h3><span class="header-section-number">18.1.1</span> 初始化 APP</h3>
<p>App 可以在任何 <code>Python path</code> 支持的路径中存放，可通过如下命令查看当前 Python 支持的路径：</p>
<div class="sourceCode" id="cb279"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb279-1"><a href="quicktutorial.html#cb279-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="im">import</span> sys</span>
<span id="cb279-2"><a href="quicktutorial.html#cb279-2"></a><span class="op">&gt;&gt;&gt;</span> sys.path</span>
<span id="cb279-3"><a href="quicktutorial.html#cb279-3"></a>[<span class="st">&#39;&#39;</span>, <span class="st">&#39;/pyweb/lib/python3.7/site-packages&#39;</span>]</span></code></pre></div>
<p>为了方便，我们在 <code>manage.py</code> 所在的目录中创建 app，这样我们就可以在之后将 app 作为顶级模块导入。进入到项目目录，执行如下操作：</p>
<div class="sourceCode" id="cb280"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb280-1"><a href="quicktutorial.html#cb280-1"></a><span class="ex">python</span> manage.py startapp polls</span></code></pre></div>
<p>执行之后，目录结构如下：</p>
<div class="sourceCode" id="cb281"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb281-1"><a href="quicktutorial.html#cb281-1"></a>├── <span class="ex">db.sqlite3</span></span>
<span id="cb281-2"><a href="quicktutorial.html#cb281-2"></a>├── <span class="ex">manage.py</span></span>
<span id="cb281-3"><a href="quicktutorial.html#cb281-3"></a>├── <span class="ex">mysite</span></span>
<span id="cb281-4"><a href="quicktutorial.html#cb281-4"></a>│   ├── <span class="ex">__init__.py</span></span>
<span id="cb281-5"><a href="quicktutorial.html#cb281-5"></a>│   ├── <span class="ex">settings.py</span></span>
<span id="cb281-6"><a href="quicktutorial.html#cb281-6"></a>│   ├── <span class="ex">urls.py</span></span>
<span id="cb281-7"><a href="quicktutorial.html#cb281-7"></a>│   └── <span class="ex">wsgi.py</span></span>
<span id="cb281-8"><a href="quicktutorial.html#cb281-8"></a>└── <span class="ex">polls</span></span>
<span id="cb281-9"><a href="quicktutorial.html#cb281-9"></a>    ├── <span class="ex">__init__.py</span></span>
<span id="cb281-10"><a href="quicktutorial.html#cb281-10"></a>    ├── <span class="ex">admin.py</span></span>
<span id="cb281-11"><a href="quicktutorial.html#cb281-11"></a>    ├── <span class="ex">apps.py</span></span>
<span id="cb281-12"><a href="quicktutorial.html#cb281-12"></a>    ├── <span class="ex">migrations</span></span>
<span id="cb281-13"><a href="quicktutorial.html#cb281-13"></a>    │   └── <span class="ex">__init__.py</span></span>
<span id="cb281-14"><a href="quicktutorial.html#cb281-14"></a>    ├── <span class="ex">models.py</span></span>
<span id="cb281-15"><a href="quicktutorial.html#cb281-15"></a>    ├── <span class="ex">tests.py</span></span>
<span id="cb281-16"><a href="quicktutorial.html#cb281-16"></a>    └── <span class="ex">views.py</span></span></code></pre></div>
<p>我们可以看到，名为 <code>polls</code> 的 app 目录下，也有视图、模型、后台管理等默认文件，方便 app 在其他项目中的重用。</p>
</div>
<div id="将应用添加到项目中" class="section level3">
<h3><span class="header-section-number">18.1.2</span> 将应用添加到项目中</h3>
<p>Django 应用是“可插拔”的。你可以在多个项目中使用同一个应用。除此之外，你还可以发布自己的应用，因为它们并不会被绑定到当前安装的 Django 上。</p>
<p>为了在我们的项目中包含应用，我们需要在配置类 <code>INSTALLED_APPS</code> 中添加设置。因为 <code>PollsConfig</code> 类写在文件 <code>polls/apps.py</code> 中，所以它的点式路径是 <code>'polls.apps.PollsConfig'</code>。在文件 <code>mysite/settings.py</code> 中 <code>INSTALLED_APPS</code> 子项添加点式路径后，它看起来像这样：</p>
<div class="sourceCode" id="cb282"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb282-1"><a href="quicktutorial.html#cb282-1"></a>INSTALLED_APPS <span class="op">=</span> [</span>
<span id="cb282-2"><a href="quicktutorial.html#cb282-2"></a>    <span class="st">&#39;polls.apps.PollsConfig&#39;</span>,</span>
<span id="cb282-3"><a href="quicktutorial.html#cb282-3"></a>    <span class="st">&#39;django.contrib.admin&#39;</span>,</span>
<span id="cb282-4"><a href="quicktutorial.html#cb282-4"></a>    <span class="st">&#39;django.contrib.auth&#39;</span>,</span>
<span id="cb282-5"><a href="quicktutorial.html#cb282-5"></a>    <span class="st">&#39;django.contrib.contenttypes&#39;</span>,</span>
<span id="cb282-6"><a href="quicktutorial.html#cb282-6"></a>    <span class="st">&#39;django.contrib.sessions&#39;</span>,</span>
<span id="cb282-7"><a href="quicktutorial.html#cb282-7"></a>    <span class="st">&#39;django.contrib.messages&#39;</span>,</span>
<span id="cb282-8"><a href="quicktutorial.html#cb282-8"></a>    <span class="st">&#39;django.contrib.staticfiles&#39;</span>,</span>
<span id="cb282-9"><a href="quicktutorial.html#cb282-9"></a>]</span></code></pre></div>
<p>现在你的 Django 项目会包含 <code>polls</code> 应用。</p>
</div>
<div id="创建视图" class="section level3">
<h3><span class="header-section-number">18.1.3</span> 创建视图</h3>
<p>在当前 app 的<code>views.py</code>中可以设置视图内容，如：</p>
<div class="sourceCode" id="cb283"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb283-1"><a href="quicktutorial.html#cb283-1"></a><span class="im">from</span> django.http <span class="im">import</span> HttpResponse</span>
<span id="cb283-2"><a href="quicktutorial.html#cb283-2"></a><span class="kw">def</span> index(request):</span>
<span id="cb283-3"><a href="quicktutorial.html#cb283-3"></a>    <span class="cf">return</span> HttpResponse(<span class="st">&quot;这是我的第一个 Django 视图&quot;</span>)</span></code></pre></div>
<p>有了视图要输出的内容之后，还需要将一个 URL 映射到这个视图。</p>
</div>
<div id="规划路由" class="section level3">
<h3><span class="header-section-number">18.1.4</span> 规划路由</h3>
<p>简洁优雅的 URL 规划对于一个高质量 Web 应用来说至关重要，为了设计你的 URL，你需要创建一个称为 <code>URL configuration</code>（简称 <code>URLConf</code>）的 Python 模块(通常为<code>urls.py</code>)，在该模块中设置 URL 模式和处理访问的函数，即用户通过什么地址访问，这个地址使用哪个模块响应。</p>
<div id="设置应用的路由规则" class="section level4">
<h4><span class="header-section-number">18.1.4.1</span> 设置应用的路由规则</h4>
<p>为了创造路由信息，我们在 <code>polls</code> 目录中，创建一个名为 <code>urls.py</code> 的文件，并在该文件中输入如下代码：</p>
<div class="sourceCode" id="cb284"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb284-1"><a href="quicktutorial.html#cb284-1"></a><span class="im">from</span> django.urls <span class="im">import</span> path</span>
<span id="cb284-2"><a href="quicktutorial.html#cb284-2"></a><span class="im">from</span> . <span class="im">import</span> views</span>
<span id="cb284-3"><a href="quicktutorial.html#cb284-3"></a></span>
<span id="cb284-4"><a href="quicktutorial.html#cb284-4"></a>urlpatterns <span class="op">=</span> [</span>
<span id="cb284-5"><a href="quicktutorial.html#cb284-5"></a>    path(<span class="st">&#39;&#39;</span>, views.index, name<span class="op">=</span><span class="st">&#39;index&#39;</span>),</span>
<span id="cb284-6"><a href="quicktutorial.html#cb284-6"></a>]</span></code></pre></div>
<p>在这段代码中，引入了框架中的 path() 方法，该方法用以设定路由的具体规则。具体到上述代码中，设定的规则为：进入到当前的访问信息，使用 views.index 模块去处理</p>
</div>
<div id="path-函数详解" class="section level4">
<h4><span class="header-section-number">18.1.4.2</span> path() 函数详解</h4>
<p>函数 <code>path()</code> 具有四个参数，两个必须参数：<code>route</code> 和 <code>view</code>，两个可选参数：<code>kwargs</code> 和 <code>name</code>。</p>
<div id="route" class="section level5">
<h5><span class="header-section-number">18.1.4.2.1</span> route</h5>
<p><code>route</code> 是一个匹配 URL 的准则（类似正则表达式）。当 Django 响应一个请求时，它会从 urlpatterns 的第一项开始，按顺序依次匹配列表中的项，直到找到匹配的项。</p>
<p>这些准则不会匹配 GET 和 POST 参数或域名。例如，URLconf 在处理请求 <a href="https://www.example.com/myapp/" class="uri">https://www.example.com/myapp/</a> 时，它会尝试匹配 <code>myapp/</code> 。处理请求 <a href="https://www.example.com/myapp/?page=3" class="uri">https://www.example.com/myapp/?page=3</a> 时，也只会尝试匹配 <code>myapp/</code>。</p>
</div>
<div id="view" class="section level5">
<h5><span class="header-section-number">18.1.4.2.2</span> view</h5>
<p>当 Django 找到了一个匹配的准则，就会调用这个特定的视图函数，并传入一个 HttpRequest 对象作为第一个参数，被“捕获”的参数以关键字参数的形式传入。</p>
</div>
<div id="kwargs" class="section level5">
<h5><span class="header-section-number">18.1.4.2.3</span> kwargs</h5>
<p>任意个关键字参数可以作为一个字典传递给目标视图函数。</p>
</div>
<div id="name" class="section level5">
<h5><span class="header-section-number">18.1.4.2.4</span> name</h5>
<p>为你的 URL 取名能使你在 Django 的任意地方唯一地引用它，尤其是在模板中。这个特性允许你只改一个文件就能全局地修改某个 URL 模式。</p>
</div>
</div>
<div id="设置项目的路由规则" class="section level4">
<h4><span class="header-section-number">18.1.4.3</span> 设置项目的路由规则</h4>
<p>下一步是要在根 <code>URLconf</code> 文件中指定我们创建的 <code>polls.urls</code> 模块。在 <code>mysite/urls.py</code> 文件的 <code>urlpatterns</code> 列表里插入一个 <code>include()</code>， 如下：</p>
<div class="sourceCode" id="cb285"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb285-1"><a href="quicktutorial.html#cb285-1"></a><span class="im">from</span> django.contrib <span class="im">import</span> admin</span>
<span id="cb285-2"><a href="quicktutorial.html#cb285-2"></a><span class="im">from</span> django.urls <span class="im">import</span> include, path</span>
<span id="cb285-3"><a href="quicktutorial.html#cb285-3"></a></span>
<span id="cb285-4"><a href="quicktutorial.html#cb285-4"></a>urlpatterns <span class="op">=</span> [</span>
<span id="cb285-5"><a href="quicktutorial.html#cb285-5"></a>    path(<span class="st">&#39;polls/&#39;</span>, include(<span class="st">&#39;polls.urls&#39;</span>)),</span>
<span id="cb285-6"><a href="quicktutorial.html#cb285-6"></a>    path(<span class="st">&#39;admin/&#39;</span>, admin.site.urls),</span>
<span id="cb285-7"><a href="quicktutorial.html#cb285-7"></a>]</span></code></pre></div>
<p>当包括其它 URL 模式时你应该总是使用 <code>include()</code> ，<code>admin.site.urls</code> 是唯一例外。</p>
<p>函数 <code>include()</code> 允许引用其它 <code>URLconfs</code>。每当 Django 遇到：<code>func：~django.urls.include</code> 时，它会截断与此项匹配的 URL 的部分，并将剩余的字符串发送到 URLconf 以供进一步处理。</p>
<p>使用 <code>include()</code> 的理念是使其可以即插即用。因为投票应用有它自己的 URLconf( polls/urls.py )，他们能够被放在 “/polls/” ， “/fun_polls/” ，“/content/polls/”，或者其他任何路径下，这个应用都能够正常工作，这是提高代码重用率的措施之一。</p>
</div>
</div>
<div id="预览效果" class="section level3">
<h3><span class="header-section-number">18.1.5</span> 预览效果</h3>
<p>经过上面的步骤后，已经创建了一个应用的首页视图和路由规则，现在是检验它们的时候了：</p>
<div class="sourceCode" id="cb286"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb286-1"><a href="quicktutorial.html#cb286-1"></a><span class="ex">python</span> manage.py runserver</span></code></pre></div>
<p>用你的浏览器访问 <a href="http://localhost:8000/polls/" class="uri">http://localhost:8000/polls/</a>，你应该能够看见在<code>index</code>视图中定义的内容了。</p>
</div>
</div>
<div id="模型的创建及使用" class="section level2">
<h2><span class="header-section-number">18.2</span> 模型的创建及使用</h2>
<p>Django 框架使用类似与 MVC 模式的方式进行信息的处理，在模型（model）中存储数据和处理数据，和数据库进行交互，因此需要配置数据库、时区等等必要信息。这些信息可通过项目的配置模块 <code>settings.py</code> 文件进行设置。</p>
<div id="配置项目信息" class="section level3">
<h3><span class="header-section-number">18.2.1</span> 配置项目信息</h3>
<div id="设置时区" class="section level4">
<h4><span class="header-section-number">18.2.1.1</span> 设置时区</h4>
<p>打开项目中的 <code>settings.py</code> 文件，修改其中的时区设置信息：</p>
<div class="sourceCode" id="cb287"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb287-1"><a href="quicktutorial.html#cb287-1"></a>TIME_ZONE <span class="op">=</span> <span class="st">&#39;Asia/Shanghai&#39;</span></span></code></pre></div>
<p>现在启动Django内置服务器，可以看到，项目的时间不再是UTC时间，而是本地时间了。</p>
</div>
<div id="设置数据库信息" class="section level4">
<h4><span class="header-section-number">18.2.1.2</span> 设置数据库信息</h4>
<p>Django 的默认数据库为 SQLite，无需额外安装软件和库，直接可以使用。当然，我们也可以使用其它的数据库存储信息。在学习阶段，我们先使用默认 SQLite 作为数据库，将学习重点先放到模型的创建和使用上来。所以，我们保持数据库的默认设置不变。</p>
<div class="sourceCode" id="cb288"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb288-1"><a href="quicktutorial.html#cb288-1"></a>DATABASES <span class="op">=</span> {</span>
<span id="cb288-2"><a href="quicktutorial.html#cb288-2"></a>    <span class="st">&#39;default&#39;</span>: {</span>
<span id="cb288-3"><a href="quicktutorial.html#cb288-3"></a>        <span class="st">&#39;ENGINE&#39;</span>: <span class="st">&#39;django.db.backends.sqlite3&#39;</span>,</span>
<span id="cb288-4"><a href="quicktutorial.html#cb288-4"></a>        <span class="st">&#39;NAME&#39;</span>: os.path.join(BASE_DIR, <span class="st">&#39;db.sqlite3&#39;</span>),</span>
<span id="cb288-5"><a href="quicktutorial.html#cb288-5"></a>    }</span>
<span id="cb288-6"><a href="quicktutorial.html#cb288-6"></a>}</span></code></pre></div>
</div>
</div>
<div id="创建模型" class="section level3">
<h3><span class="header-section-number">18.2.2</span> 创建模型</h3>
<p>模型是真实数据的简单明确的描述。它包含了储存的数据所必要的字段和行为。我们想开发的投票app需要问题（Question）和选项（Choice）两个模型，问题模型包括问题描述、发布时间，选项模型需要选项描述和当前票数。每个选项属于一个问题。模型数据关系图<a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a>如下：</p>
<div class="figure">
<img src="images/django-entry.png" alt="" />
<p class="caption">模型数据关系图</p>
</div>
<p>在应用的<code>model.py</code>中写入如下代码：</p>
<div class="sourceCode" id="cb289"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb289-1"><a href="quicktutorial.html#cb289-1"></a><span class="im">from</span> django.db <span class="im">import</span> models</span>
<span id="cb289-2"><a href="quicktutorial.html#cb289-2"></a></span>
<span id="cb289-3"><a href="quicktutorial.html#cb289-3"></a><span class="kw">class</span> Question(models.Model):</span>
<span id="cb289-4"><a href="quicktutorial.html#cb289-4"></a>    question_text <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">200</span>)</span>
<span id="cb289-5"><a href="quicktutorial.html#cb289-5"></a>    pub_date <span class="op">=</span> models.DateTimeField(<span class="st">&#39;date published&#39;</span>)</span>
<span id="cb289-6"><a href="quicktutorial.html#cb289-6"></a></span>
<span id="cb289-7"><a href="quicktutorial.html#cb289-7"></a><span class="kw">class</span> Choice(models.Model):</span>
<span id="cb289-8"><a href="quicktutorial.html#cb289-8"></a>    question <span class="op">=</span> models.ForeignKey(Question, on_delete<span class="op">=</span>models.CASCADE)</span>
<span id="cb289-9"><a href="quicktutorial.html#cb289-9"></a>    choice_text <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">200</span>)</span>
<span id="cb289-10"><a href="quicktutorial.html#cb289-10"></a>    votes <span class="op">=</span> models.IntegerField(default<span class="op">=</span><span class="dv">0</span>)</span></code></pre></div>
<p>上述代码的作用，就是设置数据库中表的字段信息（字段名称、字段类型、字段属性等等）。</p>
</div>
<div id="激活模型" class="section level3">
<h3><span class="header-section-number">18.2.3</span> 激活模型</h3>
<p>有了字段信息，可以使用Django提供的指令，生成数据模型。</p>
<div class="sourceCode" id="cb290"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb290-1"><a href="quicktutorial.html#cb290-1"></a><span class="ex">python</span> manage.py makemigrations polls</span></code></pre></div>
<p>你将会看到类似于下面这样的输出：</p>
<div class="sourceCode" id="cb291"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb291-1"><a href="quicktutorial.html#cb291-1"></a><span class="ex">Migrations</span> for <span class="st">&#39;polls&#39;</span>:</span>
<span id="cb291-2"><a href="quicktutorial.html#cb291-2"></a>  <span class="ex">polls/migrations</span>/0001_initial.py:</span>
<span id="cb291-3"><a href="quicktutorial.html#cb291-3"></a>    <span class="ex">-</span> Create model Choice</span>
<span id="cb291-4"><a href="quicktutorial.html#cb291-4"></a>    <span class="ex">-</span> Create model Question</span></code></pre></div>
<p>通过运行 <code>makemigrations</code> 命令，Django 会检测你对模型文件的修改（在这种情况下，你已经取得了新的），并且把修改的部分储存为一次<strong>迁移</strong>。所谓迁移，就是数据库结构变化的储存形式。</p>
<p>但是我们发现内置服务器输出一些警告信息：</p>
<div class="sourceCode" id="cb292"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb292-1"><a href="quicktutorial.html#cb292-1"></a><span class="ex">You</span> have 17 unapplied migration(s)<span class="bu">.</span> <span class="ex">Your</span> project may not work properly until you apply the migrations for app(s)<span class="bu">:</span> admin, auth, contenttypes, sessions.</span>
<span id="cb292-2"><a href="quicktutorial.html#cb292-2"></a><span class="ex">Run</span> <span class="st">&#39;python manage.py migrate&#39;</span> to apply them.</span></code></pre></div>
<p>以上信息的意思是项目有一些迁移，但这些迁移没有执行，可能对运行产生影响。要执行迁移，就需要使用<code>migrate</code>指令：</p>
<div class="sourceCode" id="cb293"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb293-1"><a href="quicktutorial.html#cb293-1"></a><span class="ex">python</span> manage.py migrate</span></code></pre></div>
<p>在终端可以看到输出如下信息：</p>
<div class="sourceCode" id="cb294"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb294-1"><a href="quicktutorial.html#cb294-1"></a><span class="ex">python</span> manage.py migrate</span>
<span id="cb294-2"><a href="quicktutorial.html#cb294-2"></a><span class="ex">Operations</span> to perform:</span>
<span id="cb294-3"><a href="quicktutorial.html#cb294-3"></a>  <span class="ex">Apply</span> all migrations: admin, auth, contenttypes, sessions</span>
<span id="cb294-4"><a href="quicktutorial.html#cb294-4"></a><span class="ex">Running</span> migrations:</span>
<span id="cb294-5"><a href="quicktutorial.html#cb294-5"></a>  <span class="ex">Applying</span> contenttypes.0001_initial... OK</span>
<span id="cb294-6"><a href="quicktutorial.html#cb294-6"></a>  <span class="ex">Applying</span> auth.0001_initial... OK</span>
<span id="cb294-7"><a href="quicktutorial.html#cb294-7"></a>  <span class="ex">Applying</span> admin.0001_initial... OK</span>
<span id="cb294-8"><a href="quicktutorial.html#cb294-8"></a>  <span class="ex">Applying</span> admin.0002_logentry_remove_auto_add... OK</span>
<span id="cb294-9"><a href="quicktutorial.html#cb294-9"></a>  <span class="ex">Applying</span> admin.0003_logentry_add_action_flag_choices... OK</span>
<span id="cb294-10"><a href="quicktutorial.html#cb294-10"></a>  <span class="ex">Applying</span> contenttypes.0002_remove_content_type_name... OK</span>
<span id="cb294-11"><a href="quicktutorial.html#cb294-11"></a>  <span class="ex">Applying</span> auth.0002_alter_permission_name_max_length... OK</span>
<span id="cb294-12"><a href="quicktutorial.html#cb294-12"></a>  <span class="ex">Applying</span> auth.0003_alter_user_email_max_length... OK</span>
<span id="cb294-13"><a href="quicktutorial.html#cb294-13"></a>  <span class="ex">Applying</span> auth.0004_alter_user_username_opts... OK</span>
<span id="cb294-14"><a href="quicktutorial.html#cb294-14"></a>  <span class="ex">Applying</span> auth.0005_alter_user_last_login_null... OK</span>
<span id="cb294-15"><a href="quicktutorial.html#cb294-15"></a>  <span class="ex">Applying</span> auth.0006_require_contenttypes_0002... OK</span>
<span id="cb294-16"><a href="quicktutorial.html#cb294-16"></a>  <span class="ex">Applying</span> auth.0007_alter_validators_add_error_messages... OK</span>
<span id="cb294-17"><a href="quicktutorial.html#cb294-17"></a>  <span class="ex">Applying</span> auth.0008_alter_user_username_max_length... OK</span>
<span id="cb294-18"><a href="quicktutorial.html#cb294-18"></a>  <span class="ex">Applying</span> auth.0009_alter_user_last_name_max_length... OK</span>
<span id="cb294-19"><a href="quicktutorial.html#cb294-19"></a>  <span class="ex">Applying</span> auth.0010_alter_group_name_max_length... OK</span>
<span id="cb294-20"><a href="quicktutorial.html#cb294-20"></a>  <span class="ex">Applying</span> auth.0011_update_proxy_permissions... OK</span>
<span id="cb294-21"><a href="quicktutorial.html#cb294-21"></a>  <span class="ex">Applying</span> sessions.0001_initial... OK</span></code></pre></div>
<p>现在再启动内置服务器，之前的相关警告信息消失了。</p>
</div>
<div id="数据库的生成与迁移" class="section level3">
<h3><span class="header-section-number">18.2.4</span> 数据库的生成与迁移</h3>
<p>迁移是非常强大的功能，它能让你在开发过程中持续的改变数据库结构而不需要重新删除和创建表，迁移使得开发人员专注于使数据库平滑升级而不会丢失数据。改变模型需要这三步：</p>
<ol style="list-style-type: decimal">
<li>编辑 <code>models.py</code> 文件，改变模型。</li>
<li>运行 <code>python manage.py makemigrations</code> 为模型的改变生成迁移文件。</li>
<li>运行 <code>python manage.py migrate</code> 来应用数据库迁移。</li>
</ol>
<p>数据库迁移被分解成生成和应用两个命令是为了让你能够在代码控制系统上提交迁移数据并使其能在多个应用里使用；这不仅仅会让开发更加简单，也给别的开发者和生产环境中的使用带来方便。</p>
</div>
</div>
<div id="后台管理" class="section level2">
<h2><span class="header-section-number">18.3</span> 后台管理</h2>
<p>Django 全自动地根据模型创建后台界面。管理界面不是为了网站的访问者，而是为管理者准备的。</p>
<div id="创建管理员账号" class="section level3">
<h3><span class="header-section-number">18.3.1</span> 创建管理员账号</h3>
<p>Django 提供了功能全面的后台管理程序，首先我们增加超级管理员：</p>
<div class="sourceCode" id="cb295"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb295-1"><a href="quicktutorial.html#cb295-1"></a><span class="ex">python</span> manage.py createsuperuser</span></code></pre></div>
<p>输入用户名、邮箱、密码等信息后，Django会自动生成管理员账户信息。</p>
</div>
<div id="添加引用到后台管理" class="section level3">
<h3><span class="header-section-number">18.3.2</span> 添加引用到后台管理</h3>
<p>打开 <code>polls/admin.py</code> 文件，在其中添加需要在后台管理的应用：</p>
<div class="sourceCode" id="cb296"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb296-1"><a href="quicktutorial.html#cb296-1"></a><span class="im">from</span> django.contrib <span class="im">import</span> admin</span>
<span id="cb296-2"><a href="quicktutorial.html#cb296-2"></a></span>
<span id="cb296-3"><a href="quicktutorial.html#cb296-3"></a><span class="im">from</span> .models <span class="im">import</span> Question</span>
<span id="cb296-4"><a href="quicktutorial.html#cb296-4"></a></span>
<span id="cb296-5"><a href="quicktutorial.html#cb296-5"></a>admin.site.register(Question)</span></code></pre></div>
</div>
<div id="进入后台管理界面" class="section level3">
<h3><span class="header-section-number">18.3.3</span> 进入后台管理界面</h3>
<div class="sourceCode" id="cb297"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb297-1"><a href="quicktutorial.html#cb297-1"></a><span class="ex">python</span> manage.py runserve</span></code></pre></div>
<p>在浏览器中<code>http://127.0.0.1:8000/admin/</code> 。你应该会看见管理员登录界面，输入之前设置的管理员信息后，就可以在后台管理界面中对数据进行操作。</p>
</div>
</div>
<div id="创建视图-1" class="section level2">
<h2><span class="header-section-number">18.4</span> 创建视图</h2>
<p>Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」。在 Django 中，网页和其他内容都是从视图派生而来。每一个视图表现为一个简单的 Python 函数（或者说方法，如果是在基于类的视图里的话）。Django 将会根据用户请求的 URL 来选择使用哪个视图（更准确的说，是根据 URL 中域名之后的部分）。</p>
<p>为了将 URL 和视图关联起来，Django 使用了 ‘URLconfs’ 来配置。URLconf 将 URL 模式映射到视图。</p>
<div id="添加多个视图" class="section level3">
<h3><span class="header-section-number">18.4.1</span> 添加多个视图</h3>
<p>现在再创建几个显示投票信息的页面，在 polls/views.py 中添加如下代码：</p>
<div class="sourceCode" id="cb298"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb298-1"><a href="quicktutorial.html#cb298-1"></a><span class="im">from</span> django.shortcuts <span class="im">import</span> render</span>
<span id="cb298-2"><a href="quicktutorial.html#cb298-2"></a><span class="im">from</span> django.http <span class="im">import</span> HttpResponse</span>
<span id="cb298-3"><a href="quicktutorial.html#cb298-3"></a></span>
<span id="cb298-4"><a href="quicktutorial.html#cb298-4"></a><span class="kw">def</span> index(request):</span>
<span id="cb298-5"><a href="quicktutorial.html#cb298-5"></a>    <span class="cf">return</span> HttpResponse(<span class="st">&quot;这是我的第一个视图。&quot;</span>)</span>
<span id="cb298-6"><a href="quicktutorial.html#cb298-6"></a></span>
<span id="cb298-7"><a href="quicktutorial.html#cb298-7"></a><span class="kw">def</span> detail(request, question_id):</span>
<span id="cb298-8"><a href="quicktutorial.html#cb298-8"></a>    <span class="cf">return</span> HttpResponse(<span class="st">&quot;你正在查看第 </span><span class="sc">%s</span><span class="st"> 个问题&quot;</span> <span class="op">%</span> question_id)</span>
<span id="cb298-9"><a href="quicktutorial.html#cb298-9"></a></span>
<span id="cb298-10"><a href="quicktutorial.html#cb298-10"></a><span class="kw">def</span> results(request, question_id):</span>
<span id="cb298-11"><a href="quicktutorial.html#cb298-11"></a>    response <span class="op">=</span> <span class="st">&quot;你正在查看第 </span><span class="sc">%s</span><span class="st"> 个问题的选项&quot;</span></span>
<span id="cb298-12"><a href="quicktutorial.html#cb298-12"></a>    <span class="cf">return</span> HttpResponse(response <span class="op">%</span> question_id)</span>
<span id="cb298-13"><a href="quicktutorial.html#cb298-13"></a></span>
<span id="cb298-14"><a href="quicktutorial.html#cb298-14"></a><span class="kw">def</span> vote(request, question_id):</span>
<span id="cb298-15"><a href="quicktutorial.html#cb298-15"></a>    <span class="cf">return</span> HttpResponse(<span class="st">&quot;你正在填写第 </span><span class="sc">%s</span><span class="st"> 个问题的选项&quot;</span> <span class="op">%</span> question_id)</span></code></pre></div>
<p>每个视图必须要做的只有两件事：返回一个包含被请求页面内容的 HttpResponse 对象，或者抛出一个异常，比如 Http404 。至于你还想干些什么，随便你。</p>
<div id="添加路由信息" class="section level4">
<h4><span class="header-section-number">18.4.1.1</span> 添加路由信息</h4>
<p>为了显示上述视图，需要添加路由信息，方可访问。在 polls.urls 模块中，添加如下代码：</p>
<div class="sourceCode" id="cb299"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb299-1"><a href="quicktutorial.html#cb299-1"></a><span class="im">from</span> django.urls <span class="im">import</span> path</span>
<span id="cb299-2"><a href="quicktutorial.html#cb299-2"></a></span>
<span id="cb299-3"><a href="quicktutorial.html#cb299-3"></a><span class="im">from</span> . <span class="im">import</span> views</span>
<span id="cb299-4"><a href="quicktutorial.html#cb299-4"></a></span>
<span id="cb299-5"><a href="quicktutorial.html#cb299-5"></a>urlpatterns <span class="op">=</span> [</span>
<span id="cb299-6"><a href="quicktutorial.html#cb299-6"></a>    path(<span class="st">&#39;&#39;</span>, views.index, name<span class="op">=</span><span class="st">&#39;index&#39;</span>),</span>
<span id="cb299-7"><a href="quicktutorial.html#cb299-7"></a>    path(<span class="st">&#39;&lt;int:question_id&gt;/&#39;</span>, views.detail, name<span class="op">=</span><span class="st">&#39;detail&#39;</span>),</span>
<span id="cb299-8"><a href="quicktutorial.html#cb299-8"></a>    path(<span class="st">&#39;&lt;int:question_id&gt;/results/&#39;</span>, views.results, name<span class="op">=</span><span class="st">&#39;results&#39;</span>),</span>
<span id="cb299-9"><a href="quicktutorial.html#cb299-9"></a>    path(<span class="st">&#39;&lt;int:question_id&gt;/vote/&#39;</span>, views.vote, name<span class="op">=</span><span class="st">&#39;vote&#39;</span>),</span>
<span id="cb299-10"><a href="quicktutorial.html#cb299-10"></a>]</span></code></pre></div>
<p>当某人请求你网站的某一页面时——比如说， “/polls/34/” ，Django 将会载入 mysite.urls 模块，因为这在配置项 ROOT_URLCONF 中设置了。然后 Django 寻找名为 urlpatterns 变量并且按序匹配正则表达式。在找到匹配项 ‘polls/’，它切掉了匹配的文本（“polls/”），将剩余文本——“34/”，发送至 ‘polls.urls’ URLconf 做进一步处理。在这里剩余文本匹配了 ‘<int:question_id>/’，使得我们 Django 以如下形式调用 detail():</p>
<div class="sourceCode" id="cb300"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb300-1"><a href="quicktutorial.html#cb300-1"></a>detail(request<span class="op">=&lt;</span>HttpRequest <span class="bu">object</span><span class="op">&gt;</span>, question_id<span class="op">=</span><span class="dv">34</span>)</span></code></pre></div>
</div>
</div>
<div id="模版命名空间" class="section level3">
<h3><span class="header-section-number">18.4.2</span> 模版命名空间</h3>
<p>为什么要用模板？如果把页面的设计写死在视图函数的代码里，当想改变页面的样式是，就需要编辑 Python 代码。可使用 Django 的模板系统，只要创建一个视图，就可以将页面的设计从代码中分离出来。这样在逻辑上更加清晰，有利于项目的维护。</p>
<p>首先，在 polls 目录里创建一个 templates 目录。Django 将会在这个目录里查找模板文件。在你刚刚创建的 templates 目录里，再创建一个目录 polls，然后在其中新建一个文件 index.html 。换句话说，你的模板文件的路径应该是 polls/templates/polls/index.html 。因为 Django 会寻找到对应的 app_directories ，所以你只需要使用 polls/index.html 就可以引用到这一模板了。</p>
<p>虽然我们现在可以将模板文件直接放在 polls/templates 文件夹中（而不是再建立一个 polls 子文件夹），但是这样做不太好。Django 将会选择第一个匹配的模板文件，如果你有一个模板文件正好和<strong>另一个应用中的某个模板文件重名</strong>，Django 没有办法区分它们。我们需要帮助 Django 选择正确的模板，最简单的方法就是把他们放入各自的<strong>命名空间</strong>中，也就是把这些模板放入一个<strong>和自身应用重名的子文件夹</strong>里。</p>
<p>例如，<code>polls</code>的首页模板内容为：</p>
<div class="sourceCode" id="cb301"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb301-1"><a href="quicktutorial.html#cb301-1"></a>{<span class="op">%</span> <span class="cf">if</span> latest_question_list <span class="op">%</span>}</span>
<span id="cb301-2"><a href="quicktutorial.html#cb301-2"></a><span class="op">&lt;</span>ul<span class="op">&gt;</span></span>
<span id="cb301-3"><a href="quicktutorial.html#cb301-3"></a>    {<span class="op">%</span> <span class="cf">for</span> question <span class="kw">in</span> latest_question_list <span class="op">%</span>}</span>
<span id="cb301-4"><a href="quicktutorial.html#cb301-4"></a>    <span class="op">&lt;</span>li<span class="op">&gt;&lt;</span>a href<span class="op">=</span><span class="st">&quot;{</span><span class="sc">% u</span><span class="st">rl &#39;polls:detail&#39; question.id %}&quot;</span><span class="op">&gt;</span>{{ question.question_text }}<span class="op">&lt;/</span>a<span class="op">&gt;&lt;/</span>li<span class="op">&gt;</span></span>
<span id="cb301-5"><a href="quicktutorial.html#cb301-5"></a>    {<span class="op">%</span> endfor <span class="op">%</span>}</span>
<span id="cb301-6"><a href="quicktutorial.html#cb301-6"></a><span class="op">&lt;/</span>ul<span class="op">&gt;</span></span>
<span id="cb301-7"><a href="quicktutorial.html#cb301-7"></a>{<span class="op">%</span> <span class="cf">else</span> <span class="op">%</span>}</span>
<span id="cb301-8"><a href="quicktutorial.html#cb301-8"></a><span class="op">&lt;</span>p<span class="op">&gt;</span>No polls are available.<span class="op">&lt;/</span>p<span class="op">&gt;</span></span>
<span id="cb301-9"><a href="quicktutorial.html#cb301-9"></a>{<span class="op">%</span> endif <span class="op">%</span>}</span></code></pre></div>
</div>
<div id="在视图中使用模板" class="section level3">
<h3><span class="header-section-number">18.4.3</span> 在视图中使用模板</h3>
<p>在视图<code>polls/view.py</code>中，载入模板、填充上下文，再返回由它生成的<code>HTTPResponse</code>对象，这就是在视图中使用模板的常用流程，Django 为此提供了一个快捷函数：<code>render()</code></p>
<div class="sourceCode" id="cb302"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb302-1"><a href="quicktutorial.html#cb302-1"></a><span class="im">from</span> django.http <span class="im">import</span> HttpResponse</span>
<span id="cb302-2"><a href="quicktutorial.html#cb302-2"></a><span class="im">from</span> .models <span class="im">import</span> Question</span>
<span id="cb302-3"><a href="quicktutorial.html#cb302-3"></a></span>
<span id="cb302-4"><a href="quicktutorial.html#cb302-4"></a><span class="kw">def</span> index(request):</span>
<span id="cb302-5"><a href="quicktutorial.html#cb302-5"></a>    latest_question_list <span class="op">=</span> Question.objects.order_by(<span class="st">&#39;-pub_date&#39;</span>)[:<span class="dv">5</span>]</span>
<span id="cb302-6"><a href="quicktutorial.html#cb302-6"></a>    context <span class="op">=</span> {</span>
<span id="cb302-7"><a href="quicktutorial.html#cb302-7"></a>        <span class="st">&#39;latest_question_list&#39;</span>: latest_question_list,</span>
<span id="cb302-8"><a href="quicktutorial.html#cb302-8"></a>    }</span>
<span id="cb302-9"><a href="quicktutorial.html#cb302-9"></a>    <span class="cf">return</span> render(request,<span class="st">&#39;polls/index.html&#39;</span>,context)</span></code></pre></div>
</div>
<div id="url命名空间" class="section level3">
<h3><span class="header-section-number">18.4.4</span> URL命名空间</h3>
<p>在一个真实的 Django 项目中，可能会有五个，十个，二十个，甚至更多应用。Django 如何分辨重名的 URL 呢？举个例子，polls 应用有 detail 视图，可能另一个博客应用也有同名的视图。Django 如何知道 <code>{% url %}</code> 标签到底对应哪一个应用的 URL 呢？</p>
<p>答案是：在根 URLconf 中添加命名空间。在 <code>polls/urls.py</code> 文件中稍作修改，加上 <code>app_name</code> 设置命名空间：</p>
<div class="sourceCode" id="cb303"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb303-1"><a href="quicktutorial.html#cb303-1"></a>app_name <span class="op">=</span> <span class="st">&#39;polls&#39;</span></span></code></pre></div>
<p>对应的，在模板文件中，将原来的URL编码：</p>
<div class="sourceCode" id="cb304"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb304-1"><a href="quicktutorial.html#cb304-1"></a><span class="op">&lt;</span>li<span class="op">&gt;&lt;</span>a href<span class="op">=</span><span class="st">&quot;/polls/</span><span class="sc">{{</span><span class="st"> question.id </span><span class="sc">}}</span><span class="st">/&quot;</span><span class="op">&gt;</span>{{ question.question_text }}<span class="op">&lt;/</span>a<span class="op">&gt;&lt;/</span>li<span class="op">&gt;</span></span></code></pre></div>
<p>修改为：</p>
<div class="sourceCode" id="cb305"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb305-1"><a href="quicktutorial.html#cb305-1"></a><span class="op">&lt;</span>li<span class="op">&gt;&lt;</span>a href<span class="op">=</span><span class="st">&quot;{</span><span class="sc">% u</span><span class="st">rl &#39;polls:detail&#39; question.id %}&quot;</span><span class="op">&gt;</span>{{ question.question_text }}<span class="op">&lt;/</span>a<span class="op">&gt;&lt;/</span>li<span class="op">&gt;</span></span></code></pre></div>
<p>这样做的好处是，当你想改变投票详情视图的 URL，比如想改成 <code>polls/specifics/12/</code> ，你不用在模板里修改任何东西（包括其它模板），只要在 <code>polls/urls.py</code> 里稍微修改即可，实现了模板和路由的部分解耦。</p>
</div>
</div>
<div id="模型" class="section level2">
<h2><span class="header-section-number">18.5</span> 模型</h2>
<p>在 Django 中，<strong>模型</strong>在形式上是定义数据表的文件，并没有提供数据查询之类的功能，这一点，是 Django 框架非常不同与其他框架的地方，Django 将数据查询检索的功能安排在<strong>视图</strong>中。模型包含您数据的重要字段和行为。一般来说，每一个模型都映射一个数据库表。每个模型都是一个 Python 的类，这些类继承 <code>django.db.models.Model</code>，模型类的每个属性都相当于一个数据库的字段。Django 会根据字段信息，生成数据库以及访问数据库的 API。</p>
<div id="设置数据库信息-1" class="section level3">
<h3><span class="header-section-number">18.5.1</span> 设置数据库信息</h3>
<p>Django 默认使用 SQLite 数据库，该数据库性能有限。通常我们使用 MySQL/MariaDB 或者 MongoDB 之类的数据库。下面以 MySQL 为例，设置 Django 数据库信息。</p>
<ol style="list-style-type: decimal">
<li><p>安装 MySQL，并设置好数据库信息，注意数据库编码方式设置为<code>utf8mb4</code>。</p></li>
<li><p>使用 pip 安装 pymysql 库</p></li>
<li><p>在 Django 中的项目文件<code>settings.py</code>中添加如下配置信息：</p>
<div class="sourceCode" id="cb306"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb306-1"><a href="quicktutorial.html#cb306-1"></a><span class="im">import</span> pymysql</span>
<span id="cb306-2"><a href="quicktutorial.html#cb306-2"></a>pymysql.install_as_MySQLdb()</span>
<span id="cb306-3"><a href="quicktutorial.html#cb306-3"></a>DATABASES <span class="op">=</span> {</span>
<span id="cb306-4"><a href="quicktutorial.html#cb306-4"></a>     <span class="st">&#39;default&#39;</span>: {</span>
<span id="cb306-5"><a href="quicktutorial.html#cb306-5"></a>         <span class="st">&#39;ENGINE&#39;</span>: <span class="st">&#39;django.db.backends.mysql&#39;</span>,</span>
<span id="cb306-6"><a href="quicktutorial.html#cb306-6"></a>         <span class="st">&#39;NAME&#39;</span>: <span class="st">&#39;xxx&#39;</span>,  <span class="co"># 数据库名，可以自己创建用户</span></span>
<span id="cb306-7"><a href="quicktutorial.html#cb306-7"></a>         <span class="st">&#39;USER&#39;</span>: <span class="st">&#39;xxx&#39;</span>,  <span class="co"># 用户名，可以自己创建用户</span></span>
<span id="cb306-8"><a href="quicktutorial.html#cb306-8"></a>         <span class="st">&#39;PASSWORD&#39;</span>: <span class="st">&#39;xxx&#39;</span>,  <span class="co"># 密码</span></span>
<span id="cb306-9"><a href="quicktutorial.html#cb306-9"></a>         <span class="st">&#39;HOST&#39;</span>: <span class="st">&#39;192.168.1.108&#39;</span>,  <span class="co"># mysql服务所在的主机ip</span></span>
<span id="cb306-10"><a href="quicktutorial.html#cb306-10"></a>         <span class="st">&#39;PORT&#39;</span>: <span class="st">&#39;3306&#39;</span>,</span>
<span id="cb306-11"><a href="quicktutorial.html#cb306-11"></a>     }</span>
<span id="cb306-12"><a href="quicktutorial.html#cb306-12"></a> }</span></code></pre></div></li>
</ol>
</div>
<div id="创建模型-1" class="section level3">
<h3><span class="header-section-number">18.5.2</span> 创建模型</h3>
<p>模型需要继承自<code>django.db.models.Model</code>。例如：</p>
<div class="sourceCode" id="cb307"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb307-1"><a href="quicktutorial.html#cb307-1"></a><span class="im">from</span> django.db <span class="im">import</span> models</span>
<span id="cb307-2"><a href="quicktutorial.html#cb307-2"></a></span>
<span id="cb307-3"><a href="quicktutorial.html#cb307-3"></a><span class="kw">class</span> Person(models.Model):</span>
<span id="cb307-4"><a href="quicktutorial.html#cb307-4"></a>    first_name <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">30</span>)</span>
<span id="cb307-5"><a href="quicktutorial.html#cb307-5"></a>    last_name <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">30</span>)</span></code></pre></div>
<div id="字段类型" class="section level4">
<h4><span class="header-section-number">18.5.2.1</span> 字段类型</h4>
<p>Django 框架能根据模型中的字段信息自动生成如下几类信息：</p>
<ol style="list-style-type: decimal">
<li>数据表中的字段</li>
<li>表单中对应的 HTML 元素</li>
<li>后台管理中的 HTML 页面及表单</li>
</ol>
<p>在创建模型和字段的时候，和其他变量、类、对象命名一样，不能使用 Python 的关键字、保留字以及 SQL 语句中的关键字，也不能使用和 Django 框架中有冲突的名字（不能在变量名中使用两个下划线，如<code>foo__bar</code>，变量命中使用两个下划线在 Django 中有查询等功能）。</p>
<p>常用的 Django 模型字段类型有：</p>
<table>
<thead>
<tr class="header">
<th>类型</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>BooleanField</td>
<td>布尔值字段</td>
</tr>
<tr class="even">
<td>CharField</td>
<td>字符串字段</td>
</tr>
<tr class="odd">
<td>DateField</td>
<td>日期字段</td>
</tr>
<tr class="even">
<td>DateTimeField</td>
<td>日期时间字段</td>
</tr>
<tr class="odd">
<td>EmailField</td>
<td>电子邮箱字段</td>
</tr>
<tr class="even">
<td>FileField</td>
<td>文件上传字段</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="使用模型" class="section level3">
<h3><span class="header-section-number">18.5.3</span> 使用模型</h3>
<ol style="list-style-type: decimal">
<li><p>定义好模型后，需要修改设置文件中的 <code>INSTALLED_APPS</code> ，在这个设置中添加包含你 <code>models.py</code> 文件的模块的名字。</p></li>
<li><p>在命令行中运行<code>python manage.py makemigrations</code>，创建数据库迁移文件。</p></li>
<li><p>执行数据库迁移：</p>
<div class="sourceCode" id="cb308"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb308-1"><a href="quicktutorial.html#cb308-1"></a><span class="ex">python</span> manage.py migrate</span></code></pre></div></li>
<li><p>如有必要，将模型在后台页面中显示出来。</p></li>
</ol>
</div>
</div>
<div id="参考文献-1" class="section level2">
<h2><span class="header-section-number">18.6</span> 参考文献</h2>
<ol style="list-style-type: decimal">
<li><a href="https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial01/">编写你的第一个 Djongo 应用</a></li>
<li><a href="https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial02/">数据库配置及模型创建</a></li>
<li><a href="https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial03/" class="uri">https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial03/</a></li>
</ol>

</div>
</div>







<div class="footnotes">
<hr />
<ol start="2">
<li id="fn2"><p>模型实体关系图使用<a href="https://editor.ponyorm.com">PonyORM在线编辑工具</a>制作。<a href="quicktutorial.html#fnref2" class="footnote-back">↩︎</a></p></li>
</ol>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="django.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page"><i class="fa fa-angle-left"></i></a>

    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/yangjh-xbmu/learningpython/edit/master/1800-django-quicktutorial.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["learningpython.pdf", "learningpython.epub"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
